package pearson.automation.forms;

import junit.framework.Assert;

import org.openqa.selenium.By;

import pearson.automation.forms.registration.RegisterStartForm;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.Label;
import framework.elements.Link;

public class StartForm extends BaseForm {

    private static Label identifier = new Label(By.id("home"), "Start page"); 
    private final Button signIn = new Button(By.linkText("Sign in"), "Sign in");
    
    private Button browse = new Button(By.linkText("Browse"), "Browse"); 
    /*
     * "Register" buttons 
     */
    private Link registerLeft = new Link(By.cssSelector("nav[class='home-cta'] a[title='Register']"), "Register (left button))");
    private Link registerTop = new Link(By.cssSelector("nav[id='top-nav'] a[title='Register']"), "Register (top button)");
    /*
     * LearnMore section
     */
    private Button createLearnMore = new Button(By.cssSelector("a[href*='create']"), "Learn More about Create");
    private Button collaborateLearnMore = new Button(By.cssSelector("a[href*='collaborate']"), "Learn More about Collaborate");
    private Button inspireLearnMore = new Button(By.cssSelector("a[href*='inspire']"), "Learn More about Inspire");
    
    /*
     * Footers
     */
    private Button legal = new Button(By.linkText("Legal"), "Legal");
    private Button privacy = new Button(By.linkText("Privacy"), "Privacy");
    private Button permissions = new Button(By.linkText("Permissions"), "Permissions");
    private Button contactUs = new Button(By.linkText("Contact us"), "Contact Us");
    private Label footerContent = new Label(By.id("content"), "page content");
    private Label contactUsContent = new Label(By.id("pearsonLogo"), "page content");
    private Label disclaimer = new Label(By.className("disclaimer"), "Disclaimer");
    
    private String legalLinkURLPart = "legal";
    private String privacyLinkURLPart = "legal/privacy";
    private String permissionsLinkURLPart = "legal/permissions";
    private String contactUsLinkURLPart = "contact-us";
    
    public StartForm() {
        super(identifier.getLocator(), identifier.getName());
    }

    public LoginForm goToLogin() {
        signIn.click();
        return new LoginForm();
    }
    
    public void expandCreation() {
        createLearnMore.click();
        new CreateForm();
    }
    
    public void expandCollaboration() {
        collaborateLearnMore.click();
        new CollaborateForm();
    }
    
    public void expandInspiration() {
        inspireLearnMore.click();
        new InspireForm();
    }
    /**
     * 
     * Classes for test LearnMore links checking
     *
     */
    private class CreateForm {
        private Label createSection = new Label(By.id("features-list-create"), "Create features");
        //private Label 
        
        public CreateForm() {
            createSection.waitAndAssertIsPresent();
        }
    }
    private class CollaborateForm {
        private Label createSection = new Label(By.id("features-list-collaborate"), "Collaborate features");
        //private Label 
        
        public CollaborateForm() {
            createSection.waitAndAssertIsPresent();
        }
    }
    private class InspireForm {
        private Label createSection = new Label(By.id("features-list-inspire"), "Inspire features");
        //private Label 
        
        public InspireForm() {
            createSection.waitAndAssertIsPresent();
        }
    }
    
    public String getLeftRegistrationLinkURL() {
        return registerLeft.getHref();    
    }
    
    public String getTopRegistrationLinkURL() {
        return registerTop.getHref();    
    }
    
    public RegisterStartForm checkRegistrationLink() {
        
        // TODO: if is logged in, sign off!!!
        info("Checking that \"Register\" links on the Home Page are valid ");
        String mainWindowHandle = browser.getDriver().getWindowHandle();
        registerLeft.clickAndWaitForNewWindow();
        browser.getDriver().close();
        browser.getDriver().switchTo().window(mainWindowHandle);

        registerTop.clickAndWaitForNewWindow();
        return new RegisterStartForm();
    }
    
    public void checkFooterLinks() {
        info("Checking 'Legal' ... 'Contact US' links");
        legal.clickAndWait();
        Assert.assertTrue("Legal page URL", browser.getDriver().getCurrentUrl().contains(legalLinkURLPart));
        footerContent.waitAndAssertIsPresent();
        browser.getDriver().navigate().back();
        browser.waitForPageToLoad();
        
        privacy.clickAndWait();
        Assert.assertTrue("Privacy page URL", browser.getDriver().getCurrentUrl().contains(privacyLinkURLPart));
        footerContent.waitAndAssertIsPresent();
        browser.getDriver().navigate().back();
        browser.waitForPageToLoad();
        
        permissions.clickAndWait();
        Assert.assertTrue("Permissions page URL", browser.getDriver().getCurrentUrl().contains(permissionsLinkURLPart));
        footerContent.waitAndAssertIsPresent();
        browser.getDriver().navigate().back();
        browser.waitForPageToLoad();
        
        contactUs.clickAndWaitForNewWindow();
        Assert.assertTrue("Contact us page URL", browser.getDriver().getCurrentUrl().contains(contactUsLinkURLPart));
        contactUsContent.waitAndAssertIsPresent();

        browser.exit();
        browser.getDriver().navigate().to(browser.getStartBrowserURL());
    }

    public BrowseForm clickBrowse() {
        // Some WebDriver exception
        try {
        Thread.sleep(1000);
        } catch (InterruptedException e) {
        e.printStackTrace();
        }
        browse.clickAndWait();
        return new BrowseForm();
    }

    public Object getBuildRevision() {
        if (disclaimer.getText().split("\n").length > 1) {
            return disclaimer.getText().split("\n")[1];
        } else {
            return null;
        }
    }
}

